namespace SimpleWebServices.Configuration
{
    using System;
    using System.Collections.ObjectModel;
    using System.Xml;
    using System.Xml.Serialization;
    using System.Xml.XPath;
    using SimpleWebServices;
    using SimpleWebServices.Web.UI.Html;
    using SimpleWebServices.Web.UI.Html.Elements;
    using Xunit;

    public sealed class HtmlElementCollectionFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<HtmlElementCollection>()
                .DerivesFrom<Collection<IElementSerializable>>()
                .IsConcreteClass()
                .IsSealed()
                .HasDefaultConstructor()
                .XmlRoot("elements")
                .Implements<IXmlSerializable>()
                .Result);
        }

        [Fact]
        public void deserialize()
        {
            HtmlElementCollection actual = (
                "<elements>" +
                    "<element type='SimpleWebServices.Web.UI.Html.Elements.Anchor, SimpleWebServices' />" +
                    "<element type='SimpleWebServices.Web.UI.Html.Elements.TextInput, SimpleWebServices' />" +
                "</elements>").Deserialize<HtmlElementCollection>();

            Assert.IsType<Anchor>(actual[0]);
            Assert.IsType<TextInput>(actual[1]);
        }

        [Fact]
        public void deserialize_whenInvalidType()
        {
            string xml = 
                "<elements>" +
                    "<element type='SimpleWebServices.Configuration.Dummy, SimpleWebServices' />" +
                "</elements>";

            Assert.Throws<InvalidOperationException>(() => xml.Deserialize<HtmlElementCollection>());
        }

        [Fact]
        public void deserializeEmpty()
        {
            HtmlElementCollection actual = "<elements />".Deserialize<HtmlElementCollection>();

            Assert.NotNull(actual);

            Assert.Empty(actual);
        }

        [Fact]
        public void serialize()
        {
            HtmlElementCollection obj = new HtmlElementCollection
            {
                new Anchor()
            };

            XPathNavigator navigator = obj.Serialize().CreateNavigator();

            Assert.True((bool)navigator.Evaluate("1=count(/elements/element[@type='SimpleWebServices.Web.UI.Html.Elements.Anchor, SimpleWebServices'])"));
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new HtmlElementCollection());
        }

        [Fact]
        public void IXmlSerializable_op_GetSchema()
        {
            Assert.Throws<NotSupportedException>(() => (new HtmlElementCollection() as IXmlSerializable).GetSchema());
        }

        [Fact]
        public void IXmlSerializable_op_ReadXml_XmlReaderNull()
        {
            Assert.Throws<ArgumentNullException>(() => (new HtmlElementCollection() as IXmlSerializable).ReadXml(null as XmlReader));
        }

        [Fact]
        public void IXmlSerializable_op_WriteXml_XmlWriterNull()
        {
            Assert.Throws<ArgumentNullException>(() => (new HtmlElementCollection() as IXmlSerializable).WriteXml(null as XmlWriter));
        }
    }
}